package idv.arthur.jenkins;

import idv.arthur.widgets.URLConnectionUtil;
import idv.arthur.widgets.Widgets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class UpdateJob {

	public static void main(String[] args) throws Exception {

		Widgets wid = new Widgets();
		String jobName ="MR_BL_ccBananna";
		String urlStr =Widgets.JENKINSURL+"job/"+jobName+"/config.xml";
		String postBuildURL = wid.APSERVER_HOST+"TriggerNext?ccccccccc";
		Map<String,Object> map = new HashMap<String,Object>();
        InputStream ins = null ;
        // 連線取得資料
        ins = URLConnectionUtil.doGet(urlStr,true,wid.JENKINS_USR,wid.JENKINS_PWD,map);
		SAXReader reader = new SAXReader();
		
		Document document = reader.read(ins);
		ins.close();
		
		Element root = document.getRootElement();

		
		root.element("postbuilders").element("jenkins.plugins.http__request.HttpRequest").element("url").setText(postBuildURL);		
		PostMethod postMethod = new PostMethod(urlStr);
		postMethod.setRequestHeader("Content-type","application/xml; charset=UTF-8");
		File tmpFile = new File("tmp"+System.currentTimeMillis()+".xml");
		FileInputStream fis = new FileInputStream( saveToFile(tmpFile, document) );
		postMethod.setRequestBody(fis);
		
		HttpClient client = new HttpClient();
		client.getParams().setAuthenticationPreemptive(true);
		client.getState().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(wid.JENKINS_USR,wid.JENKINS_PWD) );
		client.executeMethod(postMethod);
		if (fis != null) { fis.close(); }
		if (tmpFile.exists()) { tmpFile.delete(); }
	}
	
	public static File saveToFile(File storedFile, Document document) {
		if (storedFile.exists())
			storedFile.delete();

		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		XMLWriter writer = null;
		try {
			storedFile.createNewFile();

			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			fos = new FileOutputStream(storedFile);
			osw = new OutputStreamWriter(fos, Charset.forName("utf-8"));
			writer = new XMLWriter(osw, format);
			writer.write(document);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) { writer.close(); }
				if (osw != null) { osw.close(); }
				if (fos != null) { fos.close(); }
			} catch (IOException e) { e.printStackTrace();}
		}
		return storedFile;
	}

}
